<template>
  <tn-navbar :bottom-shadow="false" fixed>
    <text class="title">挪车码详情</text>
  </tn-navbar>
  <view class="container">
    <!-- 骨架屏 -->
    <template v-if="loading">
      <!-- 车辆信息卡片骨架屏 -->
      <view class="info-card">
        <view class="card-header">
          <view class="skeleton-title skeleton-animation"></view>
          <view class="quick-actions">
            <view class="skeleton-button skeleton-animation"></view>
            <view class="skeleton-button skeleton-animation"></view>
          </view>
        </view>
        
        <view class="vehicle-stats">
          <view class="stat-item">
            <view class="skeleton-stat skeleton-animation"></view>
            <view class="skeleton-label skeleton-animation"></view>
          </view>
          <view class="divider"></view>
          <view class="stat-item">
            <view class="skeleton-stat skeleton-animation"></view>
            <view class="skeleton-label skeleton-animation"></view>
          </view>
        </view>
      </view>
      
      <!-- 挪车码部分骨架屏 -->
      <view class="qrcode-card">
        <view class="section-header">
          <view class="skeleton-section-title skeleton-animation"></view>
        </view>
        
        <view class="qrcode-content">
          <view class="skeleton-qrcode skeleton-animation"></view>
          <view class="qrcode-actions">
            <view class="skeleton-action-button skeleton-animation"></view>
          </view>
        </view>
      </view>
      
      <!-- 最近通知部分骨架屏 -->
      <view class="notifications-card">
        <view class="section-header">
          <view class="skeleton-section-title skeleton-animation"></view>
        </view>
        
        <view class="notification-list">
          <view v-for="i in 2" :key="i" class="notification-item">
            <view class="notification-content">
              <view class="skeleton-notification-title skeleton-animation"></view>
              <view class="skeleton-notification-desc skeleton-animation"></view>
            </view>
            <view class="skeleton-time skeleton-animation"></view>
          </view>
        </view>
      </view>
    </template>

    <!-- 实际内容 -->
    <template v-else>
      <!-- 车辆信息卡片 -->
      <view class="info-card">
        <view class="card-header">
          <view class="license-plate-large">{{ vehicle.licensePlate }}</view>
          <view class="quick-actions">
            <view class="action-btn edit" @tap="navigateTo(`/pages/vehicle/edit?id=${vehicle.id}`)">
              <text class="iconfont icon-edit"></text>
              <text class="btn-text">编辑</text>
            </view>
            <view class="action-btn delete" @tap="showDeleteConfirm">
              <text class="iconfont icon-delete"></text>
              <text class="btn-text">删除</text>
            </view>
          </view>
        </view>
        
        <view class="vehicle-stats">
          <view class="stat-item">
            <text class="stat-value">{{ vehicle.moveCount || 0 }}</text>
            <text class="stat-label">挪车次数</text>
          </view>
          <view class="divider"></view>
          <view class="stat-item">
            <text class="stat-value">{{ vehicle.itime }}</text>
            <text class="stat-label">添加时间</text>
          </view>
        </view>
        
        <view class="remark" v-if="vehicle.remark">
          <text class="remark-text">{{ vehicle.remark }}</text>
        </view>
      </view>
      
      <!-- 挪车码部分 -->
      <view class="qrcode-card">
        <view class="section-header">
          <text class="section-title">挪车码</text>
        </view>
        
        <view class="qrcode-content">
          <view class="qrcode-preview" v-if="vehicle.qrcode_url">
            <image :src="vehicle.qrcode_url" mode="aspectFit" class="qrcode-image"></image>
          </view>
          
          <view class="qrcode-actions">
            <view v-if="!vehicle.qrcode_url"
                  class="action-button generate" 
                  @tap="navigateTo(`/pages/qrcode/index?vehicleId=${vehicle.id}`)">
              <text class="iconfont icon-qrcode"></text>
              <text class="btn-text">生成挪车码</text>
            </view>
            <view v-else 
                  class="action-button download" 
                  @tap="downloadQrcode">
              <text class="iconfont icon-download"></text>
              <text class="btn-text">下载挪车码</text>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 最近通知部分 -->
      <view class="notifications-card">
        <view class="section-header">
          <text class="section-title">最近通知</text>
        </view>
        
        <view class="notification-list">
          <view v-if="notifications.length === 0" class="empty-notice">
            <text>暂无通知记录</text>
          </view>
          <view 
            v-for="(notification, index) in notifications" 
            :key="index" 
            class="notification-item"
            @tap="navigateTo(`/pages/message/detail?id=${notification.id}`)"
          >
            <view class="notification-content">
              <text class="notification-title">{{ notification.title }}</text>
              <text class="notification-desc">{{ notification.content }}</text>
            </view>
            <text class="notification-time">{{ notification.time }}</text>
          </view>
        </view>
      </view>
    </template>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { onShow } from '@dcloudio/uni-app';
import {vehiclesDetails,vehiclesDel} from "@/common/api";

// 获取页面参数
const vehicleId = ref('');
const loading = ref(true); // 添加loading状态
onMounted(() => {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const options = currentPage.options;
  
  vehicleId.value = options.id;
  getVehicleDetail();
  getVehicleNotifications();
});

// 车辆详情
const vehicle = ref({
  id: '',
  licensePlate: '',
  remark: '',
  addTime: '2023-05-15 14:30',
  qrcode: '/static/images/qrcode-example.png',
  moveCount: 5
});

// 车辆相关通知
const notifications = ref([
  {
    id: '1',
    title: '挪车请求',
    content: '您的车辆被要求挪车，请尽快处理',
    time: '2小时前'
  },
  {
    id: '2',
    title: '挪车请求',
    content: '您的车辆被要求挪车，请尽快处理',
    time: '昨天 14:30'
  }
]);

// 获取车辆详情
const getVehicleDetail = async () => {
  loading.value = true; // 开始加载时显示骨架屏
  try {
    await uni.$appLogin;
    const data = await vehiclesDetails({id: vehicleId.value});
    vehicle.value = data;
    await getRecentNotifications();
  } catch (error) {
    console.error('获取车辆详情失败:', error);
  } finally {
    loading.value = false; // 加载完成后隐藏骨架屏
  }
};

// 获取车辆相关通知
const getVehicleNotifications = () => {
  // 实际项目中应调用API获取车辆相关通知
  // 这里使用模拟数据，已在上面定义
};

// 显示删除确认对话框
const showDeleteConfirm = () => {
  uni.showModal({
    title: '确认删除',
    content: '确定要删除该车辆吗？删除后将无法恢复。',
    confirmColor: '#ff4d4f',
    success: (res) => {
      if (res.confirm) {
        deleteVehicle();
      }
    }
  });
};

// 删除车辆
const deleteVehicle = async () => {
  uni.showLoading({
    title: '删除中...'
  });
  await vehiclesDel({id: vehicleId.value});
  // 实际项目中应调用API删除车辆
  uni.hideLoading();
  uni.showToast({
    title: '删除成功',
    icon: 'success'
  });
  // 返回上一页
  setTimeout(() => {
    uni.navigateBack();
  }, 1500);
};

// 下载二维码
const downloadQrcode = () => {
  if (!vehicle.value.qrcode_url) {
    uni.showToast({
      title: '二维码不存在',
      icon: 'none'
    });
    return;
  }
  
  uni.showLoading({
    title: '下载中...'
  });
  
  // 先下载图片到本地临时路径
  uni.downloadFile({
    url: vehicle.value.qrcode_url,
    success: (res) => {
      if (res.statusCode === 200) {
        // 保存图片到相册
        uni.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: () => {
            uni.hideLoading();
            uni.showToast({
              title: '保存成功',
              icon: 'success'
            });
          },
          fail: (err) => {
            uni.hideLoading();
            // 如果用户拒绝授权，提示用户打开权限
            if (err.errMsg.indexOf('auth deny') >= 0) {
              uni.showModal({
                title: '提示',
                content: '需要您授权保存图片到相册',
                confirmText: '去设置',
                success: (res) => {
                  if (res.confirm) {
                    uni.openSetting();
                  }
                }
              });
            } else {
              uni.showToast({
                title: '保存失败',
                icon: 'none'
              });
            }
          }
        });
      } else {
        uni.hideLoading();
        uni.showToast({
          title: '下载失败',
          icon: 'none'
        });
      }
    },
    fail: () => {
      uni.hideLoading();
      uni.showToast({
        title: '下载失败',
        icon: 'none'
      });
    }
  });
};

// 页面导航
const navigateTo = (url) => {
  uni.navigateTo({ url });
};

// 添加日期格式化函数
const formatDate = (dateStr) => {
  const date = new Date(dateStr);
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
};
</script>

<style>
.container {
  padding: 30rpx;
  background: #f5f7fa;
  min-height: 100vh;
}

.info-card {
  background: #ffffff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.license-plate-large {
  font-size: 44rpx;
  font-weight: bold;
  color: #212529;
}

.quick-actions {
  display: flex;
  gap: 24rpx;
}

.action-btn {
  display: flex;
  align-items: center;
  padding: 12rpx 24rpx;
  border-radius: 8rpx;
  font-size: 26rpx;
  transition: all 0.3s;
}

.action-btn .iconfont {
  font-size: 28rpx;
  margin-right: 8rpx;
}

.action-btn .btn-text {
  font-size: 26rpx;
}

.action-btn.edit {
  color: #2d8cf0;
  background: rgba(45, 140, 240, 0.1);
}

.action-btn.edit:active {
  background: rgba(45, 140, 240, 0.2);
}

.action-btn.delete {
  color: #ff4d4f;
  background: rgba(255, 77, 79, 0.1);
}

.action-btn.delete:active {
  background: rgba(255, 77, 79, 0.2);
}

.vehicle-stats {
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin: 20rpx 0;
  padding: 20rpx 0;
  background: #f8f9fa;
  border-radius: 12rpx;
}

.divider {
  width: 2rpx;
  height: 50rpx;
  background: #e9ecef;
}

.stat-item {
  flex: 1;
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 32rpx;
  font-weight: bold;
  color: #1aad19;
  margin-bottom: 8rpx;
}

.stat-label {
  font-size: 24rpx;
  color: #6c757d;
}

.remark {
  margin-top: 20rpx;
  padding: 16rpx 20rpx;
  background: #f8f9fa;
  border-radius: 8rpx;
}

.remark-text {
  font-size: 26rpx;
  color: #6c757d;
  line-height: 1.5;
}

.qrcode-card, .notifications-card {
  background: #ffffff;
  border-radius: 20rpx;
  padding: 40rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.section-header {
  margin-bottom: 30rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #212529;
}

.qrcode-content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.qrcode-preview {
  margin-bottom: 30rpx;
}

.qrcode-image {
  width: 400rpx;
  height: 400rpx;
  border-radius: 12rpx;
}

.qrcode-actions {
  margin-top: 30rpx;
  display: flex;
  justify-content: center;
}

.action-button {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24rpx 48rpx;
  border-radius: 12rpx;
  background: linear-gradient(135deg, #1aad19 0%, #168f15 100%);
  color: white;
  font-size: 28rpx;
  box-shadow: 0 6rpx 16rpx rgba(26, 173, 25, 0.2);
  transition: all 0.3s ease;
}

.action-button:active {
  transform: translateY(2rpx);
  box-shadow: 0 2rpx 8rpx rgba(26, 173, 25, 0.2);
}

.action-button .iconfont {
  font-size: 32rpx;
  margin-right: 12rpx;
}

.action-button .btn-text {
  font-weight: 500;
}

.notifications-card {
  background-color: #ffffff;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  padding: 30rpx;
}

.notification-list {
  min-height: 200rpx;
}

.empty-notice {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200rpx;
  color: #999;
  font-size: 28rpx;
}

.notification-item {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.notification-item:last-child {
  border-bottom: none;
}

.notification-content {
  flex: 1;
}

.notification-title {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 10rpx;
  display: block;
}

.notification-desc {
  font-size: 24rpx;
  color: #666;
  display: block;
}

.notification-time {
  font-size: 24rpx;
  color: #999;
  margin-left: 20rpx;
}
</style>

<style lang="scss">
/* 骨架屏样式 */
.skeleton-animation {
  position: relative;
  overflow: hidden;
  background: #f0f0f0;
  
  &::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      90deg,
      rgba(255, 255, 255, 0) 0%,
      rgba(255, 255, 255, 0.3) 50%,
      rgba(255, 255, 255, 0) 100%
    );
    animation: shimmer 1.5s infinite;
  }
}

/* 标题骨架屏 */
.skeleton-title {
  width: 200rpx;
  height: 44rpx;
  border-radius: 4rpx;
}

/* 按钮骨架屏 */
.skeleton-button {
  width: 120rpx;
  height: 52rpx;
  border-radius: 8rpx;
}

/* 统计数据骨架屏 */
.skeleton-stat {
  width: 80rpx;
  height: 32rpx;
  margin: 0 auto 8rpx;
  border-radius: 4rpx;
}

.skeleton-label {
  width: 100rpx;
  height: 24rpx;
  margin: 0 auto;
  border-radius: 4rpx;
}

/* 二维码骨架屏 */
.skeleton-qrcode {
  width: 400rpx;
  height: 400rpx;
  border-radius: 12rpx;
  margin-bottom: 30rpx;
}

/* 操作按钮骨架屏 */
.skeleton-action-button {
  width: 240rpx;
  height: 80rpx;
  border-radius: 12rpx;
}

/* 章节标题骨架屏 */
.skeleton-section-title {
  width: 160rpx;
  height: 32rpx;
  border-radius: 4rpx;
}

/* 通知项骨架屏 */
.skeleton-notification-title {
  width: 200rpx;
  height: 28rpx;
  border-radius: 4rpx;
  margin-bottom: 10rpx;
}

.skeleton-notification-desc {
  width: 400rpx;
  height: 24rpx;
  border-radius: 4rpx;
}

.skeleton-time {
  width: 100rpx;
  height: 24rpx;
  border-radius: 4rpx;
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}
</style> 